package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbRideDistance;
import cn.com.chengmandian.bike.mapper.EbRideDistanceMapper;
import cn.com.chengmandian.bike.service.IEbRideDistanceService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 电单车运营-骑行订单-里程累计 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-09-19
 */
@Slf4j
@Service
public class EbRideDistanceServiceImpl extends ServiceImpl<EbRideDistanceMapper, EbRideDistance> implements IEbRideDistanceService {

    @Override
    public EbRideDistance getOrderRideDistance(Integer rideOrderId, Integer bikeId) {
        return this.getOne(new LambdaQueryWrapper<EbRideDistance>()
                .eq(EbRideDistance::getOrderId, rideOrderId)
                .eq(EbRideDistance::getBikeId, bikeId)
                .orderByDesc(EbRideDistance::getId)
                .last("limit 0,1"));
    }

    @Override
    public void addDistance(Integer rideOrderId, Integer bikeId, BigDecimal startLng, BigDecimal startLat) {
        EbRideDistance rideDistance = new EbRideDistance();
        rideDistance.setOrderId(rideOrderId);
        rideDistance.setBikeId(bikeId);
        rideDistance.setStartLng(startLng);
        rideDistance.setStartLat(startLat);
        rideDistance.setDistance(CommonConstant.ZERO);
        rideDistance.setCreateTime(DateUtil.date());
        this.save(rideDistance);
    }

    @Override
    public void orderClose(Integer rideOrderId, Integer bikeId) {
        try {
            EbRideDistance rideDistance = this.getOrderRideDistance(rideOrderId, bikeId);
            if (rideDistance == null
                    || rideDistance.getStatus().equals(CommonConstant.TWO)) return;

            this.update(new LambdaUpdateWrapper<EbRideDistance>()
                    .set(EbRideDistance::getStatus, CommonConstant.TWO)
                    .eq(EbRideDistance::getId, rideDistance.getId()));
        }catch (Exception ex) {
            log.info("订单骑行距离计算关闭失败:{}", ex.getMessage());
        }
    }

    @Override
    public Integer getDistance(Integer rideOrderId, Integer bikeId, BigDecimal startLng, BigDecimal startLat) {
        try {
            EbRideDistance rideDistance = this.getOrderRideDistance(rideOrderId, bikeId);
            if(rideDistance == null) {
                log.error("订单骑行距离获取失败: rideDistance is null");
                return CommonConstant.ZERO;
            }
            return rideDistance.getDistance() == null? CommonConstant.ZERO : rideDistance.getDistance();
        }catch (Exception ex) {
            log.info("订单骑行距离获取失败:{}", ex.getMessage());
            return CommonConstant.ZERO;
        }
    }
}
